package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.wx.wxcollect.WxCollectVO;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.service.admin.MarketCollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author LC_RSH
 * @date 2022/6/26 11:14
 */
@Service
public class MarketCollectServiceImpl implements MarketCollectService {
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    /**
     * 通过用户id和商品id展示收藏
     *
     * @param /userId valueId
     * @author LC_R-SH
     * @since 2022/06/26 11:25
     */

    @Override
    public CommonData<MarketCollect> query(Integer userId, Integer valueId, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        if (userId != 0) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != 0) {
            criteria.andValueIdEqualTo(valueId);
        }
        criteria.andDeletedEqualTo(false);
        marketCollectExample.setOrderByClause("update_time desc");
        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);
        PageInfo<MarketCollect> pageInfo = new PageInfo(marketCollects);
        return CommonData.data(pageInfo);
    }

    /**
     * 根据分页信息及type及deleted查找收藏,再根据收藏中value_id查找返回商品
     *
     * @return 分页后收藏列表
     * @author LC_R-SH
     * @since 2022/06/28 20:19
     */
    @Override
    public CommonData<MarketCollect> queryByType(Byte type, BasePageInfo info) {
        // 开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        // 新建example
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        // 新建criteria
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        // 添加type条件
        criteria.andTypeEqualTo(type);

        //  添加userid条件
        // 得到subject
        // Subject subject = SecurityUtils.getSubject();
        // // 得到当前用户对象
        // MarketUser currentUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        // // 得到userId
        // Integer userId = currentUser.getId();
        Integer userId = 1;
        criteria.andUserIdEqualTo(userId);


        // 添加未删除条件
        criteria.andDeletedEqualTo(false);
        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);
        // 新建collectVOS list
        List<WxCollectVO> collectVOS = new ArrayList<>();
        // List<MarketGoods> marketGoods = new ArrayList<>();
        // 遍历collect
        for (MarketCollect marketCollect : marketCollects) {
            // 查找对应valueId商品并插入列表
            MarketGoods goods = goodsMapper.selectByPrimaryKey(marketCollect.getValueId());
            WxCollectVO wxCollectVO = new WxCollectVO();
            wxCollectVO.setBrief(goods.getBrief());
            wxCollectVO.setPicUrl(goods.getPicUrl());
            wxCollectVO.setValueId(goods.getId());
            wxCollectVO.setName(goods.getName());
            wxCollectVO.setId(marketCollect.getId());
            wxCollectVO.setType(marketCollect.getType());
            wxCollectVO.setRetailPrice(goods.getRetailPrice());
            collectVOS.add(wxCollectVO);
            // marketGoods.add(goodsMapper.selectByPrimaryKey(marketCollect.getValueId()));
        }
        //分页打包
        PageInfo<WxCollectVO> data = new PageInfo<>(collectVOS);
        return CommonData.data(data);
    }

    /**
     * 收藏夹新增及删除 查找是否存在 不存在则新增 存在则修改状态值(deleted)
     *
     * @return 成功
     * @author LC_R-SH
     * @since 2022/06/28 21:15
     */
    @Override
    public BaseRespVo addOrDelete(MarketCollect collect) {
        // 新建example
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        // 新建criteria
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        // criteria拼接where查询条件
        criteria.andTypeEqualTo(collect.getType());
        criteria.andValueIdEqualTo(collect.getValueId());
        //  添加userid条件 暂定为1
        // 得到subject
        // Subject subject = SecurityUtils.getSubject();
        // // 得到当前用户对象
        // MarketUser currentUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        // // 得到userId
        // Integer userId = currentUser.getId();
        Integer userId = 0;
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if (ObjectUtils.isEmpty(principals)) {
            return BaseRespVo.error(501,"please login");
        }
        MarketUser user = (MarketUser) principals.getPrimaryPrincipal();
        userId = user.getId();
        criteria.andUserIdEqualTo(userId);
        // 执行example查询语句
        List<MarketCollect> marketCollects = collectMapper.selectByExample(marketCollectExample);
        // 如果未查找到该商品 则添加该商品
        if (ObjectUtils.isEmpty(marketCollects)) {
            // 确认该商品是否存在  主键查询
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(collect.getValueId());
            // 商品不存在则返回失败信息
            if (ObjectUtils.isEmpty(marketGoods)) {
                return BaseRespVo.error("商品不存在");
            }
            // 存在则添加 补充数据
            collect.setUserId(userId);
            collect.setAddTime(new Date());
            collect.setUpdateTime(new Date());
            collect.setDeleted(false);
            // insertSelective 条件插入 空值则跳过 条件修改时因为自增id要设为bull
            collectMapper.insertSelective(collect);
        } // 查找到则修改deleted状态值 取反
        else {
            for (MarketCollect marketCollect : marketCollects) {
                // 新建marketCollect对象 用来更新数据
                MarketCollect marketCollect1 = new MarketCollect();
                // 设置对象的deleted值为原先的反值(因为只能查出来一个)
                marketCollect1.setDeleted(!marketCollect.getDeleted());
                // 利用原先的条件查询出相同的内容 更新deleted
                collectMapper.updateByExampleSelective(marketCollect1, marketCollectExample);
            }
        }
        return BaseRespVo.ok();
    }
}

/**
 * 会员收藏
 *
 * @author 86157
 * @since 2022/06/26 11:14
 */